package com.dulichdanang;

import lib.map.MyLocationListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class Map2 extends MapActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map2);
//		Location alo = new Location("A");//16.073857,108.221798
//		alo.setLatitude(16.073857);
//		alo.setLongitude(108.221798);
//		Location blo = new Location("B");
//		blo.setLatitude(16.073527);//16.073527,108.186436
//		blo.setLongitude(108.186436);
//		Log.e("space",alo.distanceTo(blo)+"");
		final TextView tv = (TextView)findViewById(R.id.tvlongla);
		//get current location
		LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
       // locManager.req
        Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateWithNewLocation(location);
        	if(location != null)                                
        		{
        			double latitude = location.getLatitude();
        			double longitude = location.getLongitude();
        			Toast.makeText(getBaseContext(),latitude+longitude+"", Toast.LENGTH_SHORT).show();
        			Log.e("haha",latitude+""+longitude);
        			tv.setText("Longla is"+longitude+""+latitude);
        		}  
        	else {
//        			Criteria criteria = new Criteria();
//        			String bestProvider = locManager.getBestProvider(criteria, false);
//        			location = locManager.getLastKnownLocation(bestProvider);
//        			double latitude = location.getLatitude();
//        			double longitude = location.getLongitude();
//        			Toast.makeText(getBaseContext(),latitude+longitude+"", Toast.LENGTH_SHORT).show();
//        			Log.e("haha2",latitude+""+longitude);
////        			tv.setText("Longla is"+longitude+""+latitude);
//        		MapView m = new MapView(this,"0sM24dnhlZU2BbGiiHgKCEmUT5Ws25FjkR7BQoA");
//        		MyLocationOverlay mlo = new MyLocationOverlay(this, m);
//        		mlo.enableCompass();
//        		mlo.enableMyLocation();
//        		GeoPoint gp = mlo.getMyLocation();
//        		tv.setText(gp.getLatitudeE6()+"-"+gp.getLongitudeE6());
        		}
	 }
	 private void updateWithNewLocation(Location location) {
         String latLongString = "";
         if (location != null) {
             double lat = location.getLatitude();
             double lng = location.getLongitude();
             latLongString = "Lat:" + lat + "\nLong:" + lng;
         } else {
             latLongString = "No location found";
         }
         Toast.makeText(getBaseContext(), latLongString+"", Toast.LENGTH_SHORT).show();
         
     }

     private final LocationListener locationListener = new LocationListener() {
         public void onLocationChanged(Location location) {
             updateWithNewLocation(location);
         }

         public void onProviderDisabled(String provider) {
             updateWithNewLocation(null);
         }

         public void onProviderEnabled(String provider) {
         }

         public void onStatusChanged(String provider, int status, Bundle extras) {
         }
     };

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map2, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
